package zhuy.use_httpclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class UseHttpClientActivity extends Activity {
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        HttpClient client = new DefaultHttpClient();
        StringBuilder builder = new StringBuilder();

        HttpGet get = new HttpGet("http://marshal.easymorse.com/wp-content/uploads/2009/10/json_demo3.txt");
        try {
            HttpResponse response = client.execute(get);
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                builder.append(s);
            }

            JSONObject jsonObject = new JSONObject(builder.toString());
            //Log.v("response", jsonObject.getString("product_name"));
            //Log.v("response", jsonObject.getString("create_time"));
            TextView text=(TextView)findViewById(R.id.text);
            text.setText(jsonObject.getString("product_name")+","+jsonObject.getString("create_time"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}